/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package beatbox;

import javafx.scene.*;
import javafx.scene.shape.*;
import javafx.scene.paint.Color;
import javafx.scene.input.*;
import javafx.scene.layout.*;
import javafx.animation.*;
import javafx.scene.control.*;
import javafx.scene.text.Font;
import javafx.scene.image.*;
import javafx.geometry.*;
import java.lang.Math;

/**
 * A container of recorded samples, assumes
 * @author Kyle
 */


public class SampleHolder extends CustomNode, Resizable {
    def MENU_HEIGHT = 28;
    def SCROLL_BAR_SPACE = 10;
    def ICON_SPACE = 10;
    var samples: SampleBox[]; //the current samples in the holder
    var selectBox: Rectangle;
    var dragging: Boolean;
    var selectedSamples: SampleBox[];
    var colorOscillator = 0;
    var icon: ImageView;
    var exampleIdInc = 0;
    public var boxSize = 100;
    public var prefWidth = 100; //width you want it to be
    public var prefHeight = 80; //height you want it to be
    var selectedOsc = Timeline {
      autoReverse: true
      keyFrames: [
        KeyFrame {
          time: 0s
          values: colorOscillator => 0
        },
        KeyFrame {
          time: .25s
          values: colorOscillator => 255 tween Interpolator.LINEAR
        }
      ]
      repeatCount: Timeline.INDEFINITE
    };



    //Effects: adds a sampleBox to the sampleHolder
    public function addSample() {
        delete samples[samples.size()-1] from samples;
        for (i in [0..10])
            insert SampleBox{
                    exampleId: exampleIdInc++;
                    boxSize: boxSize;
                    onMouseClicked:function(me:MouseEvent):Void {
                        delete (me.node as SampleBox) from samples;
                    }
                } into samples;
        if (not samples[samples.size() - 1].outline) {
            println("{samples[samples.size() - 1].outline}");
            insert SampleBox{outline: true} into samples;
        }
    }
   
    override var children =
        Group {
            focusTraversable: true;
             content: [
                 Rectangle {
                        width: bind width
                        height: bind height
                        fill: bind if (this.focused) then Color.LIGHTYELLOW else Color.WHITE;
                        stroke: Color.BLACK;
                    },
                javafx.scene.control.ScrollView {
                    blocksMouse: false
                    layoutInfo: LayoutInfo { width: bind width
                                                height: bind height - MENU_HEIGHT}
                    vbarPolicy: ScrollBarPolicy.AS_NEEDED;
                    hbarPolicy: ScrollBarPolicy.NEVER;
                    node: Flow {
                        layoutInfo: LayoutInfo { width: bind width - SCROLL_BAR_SPACE}
                        vgap: 6
                        hgap: 8
                        padding: Insets {top:5; bottom:5; left:5; right:5;}
                        content: bind samples;
                    }
                },
                selectBox = Rectangle {
                    visible: bind dragging;
                    fill: Color.YELLOW
                    stroke: Color.BLACK;
                    opacity: .5;
                }
                Button {
                    blocksMouse: false
                    font: Font.font("SansSerif", 18);
                    text: "+"
                    layoutX: 0
                    layoutY: bind this.height - this.MENU_HEIGHT;
                    action: function():Void {
                        addSample();
                    }
                    opacity: .6;
                },
                icon = ImageView {
                    translateX: bind -(icon.layoutBounds.width + ICON_SPACE);
                    image: Image {
                        url: "http://school.discoveryeducation.com/clipart/images/drum.gif"
                    }
                    fitHeight: bind height;
                    preserveRatio: true
                    scaleX: .4
                    scaleY: .4
                }
                ]
                onMouseDragged:function(me:MouseEvent):Void {
                    if (not dragging) {
                        dragging = true;
                    }
                    if (me.dragX < 0) {
                        selectBox.x = me.x;
                        selectBox.width = -me.dragX;
                    } else {
                        selectBox.x = me.dragAnchorX - this.layoutX;
                        selectBox.width = me.dragX;
                    }

                    if (me.dragY < 0) {
                        selectBox.y = me.y;
                        selectBox.height = -me.dragY;
                    } else {
                        selectBox.y = me.dragAnchorY - this.layoutY;
                        selectBox.height = me.dragY;
                    }

                    for (sb in samples) {
                        if (sb.boundsInParent.intersects(
                            sb.parent.parent.parentToLocal(
                            sb.parent.parentToLocal(selectBox.boundsInLocal)))) {
                            sb.selected = true;
                        } else
                            sb.selected = false;
                    }

                }
            }

   override var onMouseReleased = function(me:MouseEvent):Void {
            if (dragging)
                dragging = false;
    }

   override var onMousePressed = function(me:MouseEvent):Void {
            requestFocus();
    }


   override var onMouseClicked = function(me:MouseEvent):Void {
            for (sb in samples)
                sb.selected = false;
    }

   override var onKeyPressed = function(me:KeyEvent):Void {
        if (me.code.equals(KeyCode.VK_DELETE)) {
            for (i in [samples.size()..0 step -1]) {
                if (samples[i].selected) {
                    delete samples[i] from samples;
                }
            }
            if (not samples[samples.size()].outline)
                insert SampleBox{outline: true} into samples;
        }
    }

    //inherited from resizable
    override function getMinWidth():Number {
        100;
    }

    override function getMinHeight(): Number {
        80;
    }



    override function getPrefWidth(height:Number):Number {
         return prefWidth;
     }
     override function getPrefHeight(width:Number):Number {
         return prefHeight;
     }
}
